package com.dinghui.salary.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dinghui.salary.pojo.Page;
import com.dinghui.salary.pojo.SalaryType;
import com.dinghui.salary.pojo.dto.LoginDTO;
import com.dinghui.salary.pojo.dto.MouthTypeDTO;
import com.dinghui.salary.pojo.dto.SalaryTypeDTO;
import com.dinghui.salary.pojo.vo.SalaryTypeFieldVO;
import com.dinghui.salary.pojo.vo.SalaryTypeVO;
import com.dinghui.salary.pojo.vo.SelectSalaryTypeVO;
import com.dinghui.salary.service.SalaryTypeService;
import com.dinghui.salary.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author tanrentao
 * @date 2021-09-06
 * @desc 用户登录
 */
@RestController
@RequestMapping("/SalaryType")
@Api(value = "SalaryTypeController", tags = "工资类型")
public class SalaryTypeController {

    @Reference(version = "2.0.0")
    private SalaryTypeService salaryTypeService;

    @ApiOperation("增加或修改")
    @PostMapping("/add")
    public ApiResult add (@RequestBody SalaryTypeDTO dto) throws Exception{
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        SalaryType salaryType = new SalaryType();
        if(dto.getId() != null){
            salaryType.setId(dto.getId());
            salaryType.setUpdateBy(LocalUser.getUser().getId().toString());
            salaryType.setUpdateTime(formatter.format(date));
        }else{
            salaryType.setId(GetUUID.getUUID());
        }
        salaryType.setFieldDesc(dto.getFieldDesc());
        salaryType.setFieldName(dto.getFieldName());
        salaryType.setFieldType(dto.getFieldType());
        salaryType.setFieldSize(dto.getFieldSize());
        salaryType.setCreateBy(LocalUser.getUser().getId().toString());
        salaryType.setCreateTime(formatter.format(date));
        salaryType.setIsDelete(false);
        int i = 0;
        if(dto.getId() != null){
            i= salaryTypeService.modify(salaryType);
        }else{
             i= salaryTypeService.add(salaryType);
        }
        return ApiResult.success(i);
    }

    @ApiOperation("删除")
    @PostMapping("/Delete")
    public ApiResult Delete(@RequestBody SalaryTypeDTO dto) {
        SalaryType salaryType = new SalaryType();
        salaryType.setId(dto.getId());
        salaryType.setIsDelete(true);
        return ApiResult.success(salaryTypeService.Delete(salaryType));
    }

    @ApiOperation("查询工资类型列表")
    @PostMapping("/FindAll")
    public ApiResult FindAll(@RequestBody Page dto) {
        PagedGridResult pagedGridResult = salaryTypeService.FindAll(dto);
        return ApiResult.success(pagedGridResult);
    }

    @ApiOperation("根据Id查询")
    @PostMapping("/FindById")
    public ApiResult FindById(@RequestBody SalaryTypeDTO dto) {
        SalaryType salaryType = new SalaryType();
        salaryType.setId(dto.getId());
        return ApiResult.success(salaryTypeService.FindById(salaryType));
    }

    @ApiOperation("查询工资类型下拉列表")
    @GetMapping("/getSalaryTypeList")
    public ApiResult getSalaryTypeList() {
        List<SelectSalaryTypeVO> list = salaryTypeService.selectSalaryTypeList();
        return ApiResult.success(list);
    }

    @ApiOperation("根据月工资类型的时间查询类型下拉列表")

    @PostMapping("/getList")
    public ApiResult getList(@RequestBody MouthTypeDTO dto) {
        List<SelectSalaryTypeVO> list = salaryTypeService.getList(dto);
        return ApiResult.success(list);
    }

    @ApiOperation("查询所有工资字段")
    @GetMapping("/getSalaryFieldAll")
    public ApiResult getSalaryFieldAll() {
        List<SalaryTypeFieldVO> list = salaryTypeService.getSalaryFieldAll();
        return ApiResult.success(list);
    }

    @ApiOperation("根据月工资类型的时间查询类型下拉列表")
    @PostMapping("/getAll")
    public ApiResult getAll(@RequestBody Page dto) {
        PagedGridResult list = salaryTypeService.getAll(dto);
        return ApiResult.success(list);
    }

}
